//
//  ModifyEmailVC.m
//  sensen_ios
//
//  Created by mc on 16/2/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ModifyEmailVC.h"


#import "ModifyTextField.h"


@interface ModifyEmailVC ()
{
    ModifyTextField *_field; // 输入框
    UIButton *_confirmBtn; //确认按钮
}
@end

@implementation ModifyEmailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修改昵称
    self.title = @"邮箱修改";
    
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    
    // 导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];
    
    // 创建邮箱输入框
    [self _createInputTextfield];
    //  创建确定按钮
    [self _createConfirmButton];
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 导航栏样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    // 把当前邮箱显示在输入框
    UserModel *model = [CommonCache getUserInfo];
    _field.text = model.email;
}


// 创建邮箱输入框
- (void)_createInputTextfield
{
    _field = [[ModifyTextField alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 40, 40) placeholder:@"请输入您的新邮箱"];
    [self.view addSubview:_field];
}

// 创建确认修改按钮
- (void)_createConfirmButton{
    _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _confirmBtn.frame = CGRectMake(20, 100, SCREEN_WIDTH - 40, 35);
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    _confirmBtn.layer.cornerRadius = 2;
    [_confirmBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [_confirmBtn setBackgroundColor:SS_NAVI_BLUE_COLOR];
    
    _confirmBtn.layer.borderColor = SS_NAVI_BLUE_COLOR.CGColor;
    _confirmBtn.layer.borderWidth = 1;
    _confirmBtn.layer.masksToBounds = YES;
    _confirmBtn.layer.cornerRadius = 3.0;
    
}


// 保存按钮执行事件
- (void)saveAction{
    
        if (_field.text.length == 0) {
            [SVProgressHUD showOnlyStatus:@"请先输入电子邮箱" withDuration:1.5];
            return;
        }
    
        if (![BoyeTool  isValidateEmail:_field.text]) {
          [SVProgressHUD showOnlyStatus:@"请输入正确的邮箱格式" withDuration:1.5];
          return;
        }
    
      if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        // 创建警告弹窗
        NSString *alertTitle = @"确定要修改邮箱？";
        NSString *yesButtonTitle = @"确定";
        NSString *cancelButtonTitle = @"取消";
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
    
        // 添加点击动作
        // 取消
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
        // 确定
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:yesButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
                                    {
                                        
                                        [SVProgressHUD showWithStatus:@"正在修改中..."];
                                        
                                        UserModel *model = [CommonCache getUserInfo];
                                        NSDictionary *params = @{@"uid":model.uid,
                                                                 @"email":_field.text};
                                        // 发送网络请求
                                        [BoyeHttpClient post:@"BY_User_update" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
                                            [SVProgressHUD showOnlyStatus:@"修改成功!" withDuration:1];
                                            // 保存到本地
                                            model.email = _field.text;
                                            [CommonCache setUserInfo:model];
                                            // 返回上一界面
                                            [self.navigationController popViewControllerAnimated:YES];
    
                                        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            
                                            
                                            [SVProgressHUD showOnlyStatus:error.domain withDuration:1.5];
                                            
                                        }];
                                    }];
    
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:yesAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
          
      }else{
          
          [SVProgressHUD showWithStatus:@"正在修改中..."];
          
          UserModel *model = [CommonCache getUserInfo];
          NSDictionary *params = @{@"uid":model.uid,
                                   @"email":_field.text};
          // 发送网络请求
          [BoyeHttpClient post:@"BY_User_update" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
              [SVProgressHUD showOnlyStatus:@"修改成功!" withDuration:1];
              // 保存到本地
              model.email = _field.text;
              [CommonCache setUserInfo:model];
              // 返回上一界面
              [self.navigationController popViewControllerAnimated:YES];
              
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              
              
              [SVProgressHUD showOnlyStatus:error.domain withDuration:1.5];
              
          }];
      

      }


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
